package com.qingxin.qrCode.service;

import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;
import com.qingxin.qrCode.entity.QrNum;
import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.utils.BeanUtil;
/**
 * 二维码
 * @author Administrator
 *
 */
@Service
public class QrNumService extends BaseService<QrNum, String> {
	

	public SysError delete(String[] qrnumid, LoginUser loginUser, HttpServletRequest request){
		for(String id : qrnumid){
			QrNum old = getSafeEntity(id);
			if(old.getIs_activation().equals("1")){
				return new SysError("其中有二维码已经被使用");
			}else{
				QrNum qrNum = new QrNum();
				qrNum.setDeltype("1");
				String fields = "deltype";
				BeanUtil.setTargetFields(old, qrNum, fields, true);
				Updater<QrNum> updater = new Updater<>(old, fields);
				this.update(updater);
			}
		}
		return null;
	}
	
	
	/**
	 * 获取可用二维码
	 * @param companyid
	 * @param parkid
	 * @param cropid
	 * @param pageParam
	 * @return
	 */
	public Pager listBind(String companyid, String parkid, String cropid, PageParam pageParam){
		Querier querier = Querier.create("from QrNum q where q.companyid=:companyid and q.parkid=:parkid" +
				" and q.cropid=:cropid and q.deltype='0' and q.is_activation='0'" +
				" order by code");
		querier.setParam("companyid", companyid);
		querier.setParam("parkid", parkid);
		querier.setParam("cropid", cropid);
		
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}
	
	
	/**
	 * 更新指定二维码状态
	 * @param qrnumid
	 * @param is_activation 0未使用，1已使用
	 * @return
	 */
	public SysError updateActivation(String qrnumid, String is_activation){
		QrNum qrNum = getSafeEntity(qrnumid);
		if(qrNum == null){
			return new SysError("信息不存在");
		}
		qrNum.setIs_activation(is_activation);
		
		Updater<QrNum> updater = new Updater<>(qrNum, "is_activation");
		this.update(updater);
		return null;
	}
	
	
}
